<template>
  <div>
    <!--添加-->
    <el-row :gutter="10">
      <el-col :span="6">
        <el-button icon="el-icon-plus" type="primary" @click="addDialogVisible = true">
          添加标签
        </el-button>
      </el-col>
    </el-row>

    <!--    列表-->
    <el-table :data="tagList">
      <el-table-column label="序号" type="index" width="50"></el-table-column>
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="颜色">
        <template v-slot="scope">
          <span style="float: left; width: 100px">{{ scope.row.color }}</span>
          <span
            :class="`me-${scope.row.color}`"
            style="float: left; width: 100px; height: 23px"
          ></span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="primary"
            @click="showEditDialog(scope.row)"
          >
            编辑
          </el-button>
          <el-popconfirm
            icon="el-icon-delete"
            iconColor="red"
            title="确定删除吗？"
            @onConfirm="deleteTagById(scope.row.id)"
          >
            <el-button slot="reference" icon="el-icon-delete" size="mini" type="danger">
              删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
      :current-page="queryBody.pageNum"
      :page-size="queryBody.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      :total="total"
      background
      hide-on-single-page
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handlePageSizeChange"
      @current-change="handleCurrentNumChange"
    ></el-pagination>

    <!--    添加标签的对话框-->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="addDialogVisible"
      title="添加标签"
      width="50%"
      @close="closeAddDialog"
    >
      <el-form ref="addFormRef" :model="addForm" :rules="formRules" label-width="80px">
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="addForm.name" />
        </el-form-item>
        <el-form-item label="标签颜色">
          <el-select
            v-model="addForm.color"
            :clearable="true"
            placeholder="请选择颜色"
            style="width: 100%"
          >
            <el-option
              v-for="item in colors"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
              <span style="float: left; width: 100px">{{ item.label }}</span>
              <span
                :class="`me-${item.value}`"
                style="float: left; width: 100px; height: inherit"
              ></span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAddTag">确 定</el-button>
      </span>
    </el-dialog>

    <!--    编辑标签的对话框-->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="editDialogVisible"
      title="编辑标签"
      width="50%"
      @close="closeEditDialog"
    >
      <el-form ref="editFormRef" :model="editForm" :rules="formRules" label-width="80px">
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="editForm.name" />
        </el-form-item>
        <el-form-item label="标签颜色" prop="color">
          <el-select
            v-model="editForm.color"
            :clearable="true"
            placeholder="请选择颜色"
            style="width: 100%"
          >
            <el-option
              v-for="item in colors"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
              <span style="float: left; width: 100px">{{ item.label }}</span>
              <span
                :class="`me-${item.value}`"
                style="float: left; width: 100px; height: inherit"
              ></span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditTag">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { addTag, deleteTagById, editTag, getData } from '@/api/tag'
  import _ from 'lodash'

  export default {
    name: 'TagList',
    components: {},
    data() {
      return {
        tagList: [],
        queryBody: {
          pageNum: 1,
          pageSize: 10,
        },
        total: 0,
        addDialogVisible: false,
        editDialogVisible: false,
        addForm: {
          name: '',
          color: '',
        },
        editForm: {},
        formRules: {
          name: [{ required: true, message: '请输入标签名称', trigger: 'blur' }],
        },
        colors: [
          { label: '红色', value: 'red' },
          { label: '橘黄', value: 'orange' },
          { label: '黄色', value: 'yellow' },
          { label: '橄榄绿', value: 'olive' },
          { label: '纯绿', value: 'green' },
          { label: '水鸭蓝', value: 'teal' },
          { label: '纯蓝', value: 'blue' },
          { label: '紫罗兰', value: 'violet' },
          { label: '紫色', value: 'purple' },
          { label: '粉红', value: 'pink' },
          { label: '棕色', value: 'brown' },
          { label: '灰色', value: 'grey' },
          { label: '黑色', value: 'black' },
        ],
      }
    },
    computed: {},
    created() {
      this.fetchData()
    },
    methods: {
      async fetchData() {
        const res = await getData(this.queryBody)
        this.tagList = res.data.list
        this.total = res.data.total
      },
      showEditDialog(row) {
        this.editForm = _.cloneDeep(row)
        this.editDialogVisible = true
      },
      async deleteTagById(id) {
        const res = await deleteTagById(id)
        this.msgSuccess(res.msg)
        await this.fetchData()
      },
      handlePageSizeChange(newPageSize) {
        this.queryBody.pageSize = newPageSize
        this.fetchData()
      },
      handleCurrentNumChange(newCurrentNum) {
        this.queryBody.pageNum = newCurrentNum
        this.fetchData()
      },
      closeAddDialog() {
        this.addForm.color = ''
        this.$refs.addFormRef.resetFields()
      },
      handleAddTag() {
        this.$refs.addFormRef.validate(async (valid) => {
          if (valid) {
            const res = await addTag(this.addForm)
            this.msgSuccess(res.msg)
            this.addDialogVisible = false
            await this.fetchData()
          }
        })
      },
      closeEditDialog() {
        this.editForm = {}
        this.$refs.editFormRef.resetFields()
      },
      handleEditTag() {
        this.$refs.editFormRef.validate(async (valid) => {
          if (valid) {
            const res = await editTag(this.editForm)
            this.msgSuccess(res.msg)
            this.editDialogVisible = false
            await this.fetchData()
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-button + span {
    margin-left: 10px;
  }
</style>
